package org.itboys.weixin.service;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.itboys.admin.tools.AdminSessionHolder;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.weixin.entity.SendWeixinMessageDetail;
import org.springframework.stereotype.Service;

import com.google.common.collect.Maps;

/**
 * 微信消息相关
 * @author ChenJunhui
 *
 */
@Service
public class SendWeixinMessageDetailService extends BaseWeixinService<SendWeixinMessageDetail, Long> {

	private static final long serialVersionUID = -8485497513206255296L;
	
	@Resource(name="weixinDS")
	private MongoDataSource messageDetailDataSource;

	@Override
	protected MongoDataSource getMongoDataSource() {
		return messageDetailDataSource;
	}

	@Override
	protected Class<SendWeixinMessageDetail> getEntityClass() {
		return SendWeixinMessageDetail.class;
	}
	
	public List<SendWeixinMessageDetail> getByWxMsgId(Long wxMsgId){
		Map<String, Object> sqlMap=Maps.newHashMap();
		sqlMap.put("wxMsgId", wxMsgId);
		List<SendWeixinMessageDetail> list = super.list(sqlMap);
		return list;
	}
	
	public void save(SendWeixinMessageDetail detail){
		detail.setCreateTime(new Date());
		detail.setCreator(AdminSessionHolder.getAdminUserId());
		super.save(detail);
	}
	
	public void update(SendWeixinMessageDetail detail){
		SendWeixinMessageDetail detail2 = getById(detail.getId());
		detail.setCreateTime(detail2.getCreateTime());
		detail.setCreator(detail2.getCreator());
		detail.setUpdater(AdminSessionHolder.getAdminUserId());
		detail.setUpdateTime(new Date());
		if(StringUtils.isNotEmpty(detail.getImg())){
			detail.setImg(detail2.getImg());
		}
		super.update(detail);
	}
	
}
